package cn.kgc.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author wangyue
 * @version 1.0
 */
@Configuration
public class MyBatisPlusConfig  implements MetaObjectHandler {

    /**
     * 使用MyBatisPlus新增数据时,给某些自动填充值
     */
    @Override
    public void insertFill(MetaObject metaObject) {
      setFieldValByName("creationDate",new Date(),metaObject);
      setFieldValByName("version",1,metaObject);
      setFieldValByName("deleted",0,metaObject);
    }

    /**
     * 使用MyBatisPlus修改数据时,给modifyDate属性自动赋值
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("modifyDate",new Date(),metaObject);
    }

    /**
     * 分页拦截器
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

    /**
     * 乐观锁拦截器
     */
    @Bean
    public OptimisticLockerInterceptor  optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }
}